package com.example.administrator.myapplication;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.alipay.sdk.auth.AlipaySDK;
import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class Map extends Activity {
    private Handler mHandler;
    private AMap map = null;
    private ArrayList<MyLocationList> item=new ArrayList<>();
    private ArrayList<StoreAddress> item1=new ArrayList<>();
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mymap);
        MapView mapView=findViewById(R.id.mymap);
        httpContect();
        setData();
        mapView.onCreate(savedInstanceState);

        if (map==null){
            map=mapView.getMap();
        }
        myOwnLocation(map);

    }
    private void myOwnLocation(AMap map){
        MyLocationStyle myLocationStyle=new MyLocationStyle();
        myLocationStyle.interval(10000);
        myLocationStyle.showMyLocation(true);
        map.setMyLocationEnabled(true);
        map.setMyLocationStyle(myLocationStyle);
    }
    private void centerLocation(){
        for (int i=0;i<item.size();i++){
            locationPoint(i);
        }
    }
    private void storeLocation(){
        for (int i=0;i<item1.size();i++){
            storeLocationPoint(i);

        }

    }
    private void storeLocationPoint(int i){
        LatLng latLng=new LatLng(item1.get(i).V1,item1.get(i).V);
        final Marker marker=map.addMarker(new MarkerOptions().position(latLng).title(item1.get(i).name).snippet(item1.get(i).address));
        marker.isFlat();

    }
    private void locationPoint(int i){
            LatLng latLng=new LatLng(item.get(i).v1,item.get(i).v);
            final Marker marker=map.addMarker(new MarkerOptions().position(latLng).title(item.get(i).name).snippet("DefaultMarker"));
            marker.isFlat();

        }
    private void httpContect(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpContect httpContect=new HttpContect();
                String route="admin-api/map/index";
                int type=0;
                String access_token="1";
                String data="access_token="+access_token+"&type="+type;
                String JsonValue=httpContect.send(route,data);
                Message message=new Message();
                message.obj=JsonValue;
                mHandler.sendMessage(message);
            }
        }).start();
    }
    private void setData(){
        mHandler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                String data= (String) msg.obj;
                resolveJson(data);
            }
        };
    }
    private void resolveJson(String value){
        try {

            JSONObject jsonObject=new JSONObject(value);
            String message=jsonObject.optString("message");
            Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
            JSONArray  data=jsonObject.getJSONArray("data");
            item.clear();
            for (int i=0;i<=data.length()-1;i++){
                MyLocationList myLocationList=new MyLocationList();
                JSONObject jsonObject1= (JSONObject) data.get(i);
                String name=jsonObject1.optString("name");
                String center=jsonObject1.optString("center");
                 String splitV[]=center.split(",");
                 float v= Float.parseFloat(splitV[0]);
                 float v1= Float.parseFloat(splitV[1]);
                 myLocationList.setName(name);
                 myLocationList.setV(v);
                 myLocationList.setV1(v1);
                 item.add(myLocationList);
                    centerLocation();
                JSONArray subDistricts=jsonObject1.getJSONArray("subDistricts");
                for (int a=0;a<subDistricts.length();a++){
                    StoreAddress addressList=new StoreAddress();
                    JSONObject jsonObject2= (JSONObject) subDistricts.get(a);
                    String name1=jsonObject2.optString("name");
                    String address=jsonObject2.optString("address");
                    String center1=jsonObject2.optString("center");
                   String spliyte1[]= center1.split(",");
                   float V= Float.parseFloat(spliyte1[0]);
                   float V1= Float.parseFloat(spliyte1[1]);
                   addressList.setAddress(address);
                   addressList.setV(V);
                   addressList.setV1(V1);
                   addressList.setName(name1);
                   item1.add(addressList);
                    storeLocation();
                }
                Log.i("123213312","0)))))))))))))))))))))))))))))))))))))))))))))"+item.size());
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
